package www.com.library.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * 滚动页标记点
 */
public class ScrollPoints extends LinearLayout {

    private List<ImageView> points = new ArrayList<>();
    private int mImageID; // 正常情况下图标
    private int mFocusColorID; // 选中状态下图标
    private boolean hasColor = true;//标识是用颜色渲染方式还是选中图表，默认是颜色
    private int margin = 5;
    private int pointWidth = 0;
    private int pointHeight = 0;

    public ScrollPoints(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setPointMargin(int margin) {
        this.margin = margin;
    }

    public void setPointWidthHeight(int widhth, int height) {
        this.pointWidth = widhth;
        this.pointHeight = height;
    }

    /**
     * @param context       页面句柄
     * @param count         标记点个数
     * @param selected      默认选择标记点
     * @param mImageID      正常情况下图标
     * @param mFocusColorID 选中状态下图标颜色或者图片id
     * @param hasColor      标识是用颜色渲染方式还是选中图表
     */
    public void initPoints(Context context, int count, int selected, int mImageID, int mFocusColorID, boolean hasColor) {
        this.hasColor = hasColor;
        initPoints(context, count, selected, mImageID, mFocusColorID);
    }

    /**
     * 初始化水平滚动页和标记点
     *
     * @param context       页面句柄
     * @param count         标记点个数
     * @param selected      默认选择标记点
     * @param mImageID      正常情况下图标
     * @param mFocusColorID 选中状态下图标颜色或者图片id
     */
    public void initPoints(Context context, int count, int selected, int mImageID, int mFocusColorID) {
        if (count <= 1) {
            this.setVisibility(GONE);
            return;
        } else {
            setVisibility(VISIBLE);
        }
        LinearLayout pointBox = new LinearLayout(context);
        points.clear();
        this.mImageID = mImageID;
        this.mFocusColorID = mFocusColorID;

        for (int i = 0; i < count; i++) {
            ImageView slidePot = new ImageView(context);
            LayoutParams lp;
            if (pointWidth != 0 && pointHeight != 0) {
                lp = new LayoutParams(pointWidth, pointHeight);
            } else {
                lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            }
            lp.gravity = Gravity.CENTER_VERTICAL;
            lp.setMargins(margin, 0, margin, 0);
            slidePot.setLayoutParams(lp);

            if (hasColor) {
                slidePot.setImageResource(mImageID);
                if (i == selected) {
                    slidePot.setColorFilter(mFocusColorID);
                }
            } else {
                if (i == selected) {
                    slidePot.setImageResource(mFocusColorID);
                } else {
                    slidePot.setImageResource(mImageID);
                }
            }
            points.add(slidePot);
            pointBox.addView(slidePot);
        }
        removeAllViews();
        addView(pointBox);
    }

    /**
     * 更换标记点
     *
     * @param position 当前标记点
     */
    public void changeSelectedPoint(int position) {
        for (int i = 0; i < points.size(); i++) {
            ImageView point = points.get(i);
            if (hasColor) {
                point.setImageResource(mImageID);
                if (i == position) {
                    point.setColorFilter(mFocusColorID);
                } else {
                    point.setColorFilter(0);
                }
            } else {
                if (i == position) {
                    point.setImageResource(mFocusColorID);
                } else {
                    point.setImageResource(mImageID);
                }
            }
        }
    }
}
